package org.travelfusion.xmlclient.ri.handler.misc;

import java.util.HashMap;
import java.util.Map;

import javax.xml.stream.XMLStreamConstants;

import org.travelfusion.xmlclient.handler.HandlesRequestsFor;
import org.travelfusion.xmlclient.impl.handler.AbstractXmlToolRequestStAXResponseHandler;
import org.travelfusion.xmlclient.ri.xobject.misc.XAirline;
import org.travelfusion.xmlclient.ri.xobject.misc.XAirlineName;
import org.travelfusion.xmlclient.ri.xobject.misc.XGetAirlinesDataRequest;
import org.travelfusion.xmlclient.ri.xobject.misc.XGetAirlinesDataResponse;

@HandlesRequestsFor(XGetAirlinesDataRequest.class)
public class GetAirlinesDataHandler extends AbstractXmlToolRequestStAXResponseHandler<XGetAirlinesDataRequest, XGetAirlinesDataResponse> {

	@Override
	protected void fillTemplate() throws Exception {
	}

	@Override
	public XGetAirlinesDataResponse handleResponse() throws Exception {

		Map<String, XAirline> airlineMap = new HashMap<String, XAirline>();
		Map<String, XAirlineName> airlineNameMap = new HashMap<String, XAirlineName>();

		StringBuilder builder = new StringBuilder(32);

		XAirline airline = null;
		XAirlineName airlineName = null;
		String currentName = null;

		done : for (int event; ((event = responseReader.next()) != XMLStreamConstants.END_DOCUMENT);) {
			switch (event) {
				case XMLStreamConstants.START_ELEMENT :
					builder.setLength(0);
					currentName = responseReader.getName().getLocalPart();
					if (airline == null) {
						if ("Airline".equals(currentName)) {
							airline = new XAirline();
							airlineNameMap = new HashMap<String, XAirlineName>();
						}
					} else {
						if (airlineName == null) {
							if ("AirlineName".equals(currentName)) {
								airlineName = new XAirlineName();
							}
						}
					}
					break;
				case XMLStreamConstants.CHARACTERS :
					if (airline != null) {
						builder.append(responseReader.getTextCharacters(), responseReader.getTextStart(), responseReader.getTextLength());
					}
					break;
				case XMLStreamConstants.END_ELEMENT :
					if (airline != null) {
						currentName = responseReader.getName().getLocalPart();
						if (airlineName != null) {
							if ("Language".equals(currentName)) {
								airlineName.setLanguage(builder.toString().trim());
							} else if ("LanguageCode".equals(currentName)) {
								airlineName.setLanguageCode(builder.toString().trim());
							} else if ("Name".equals(currentName)) {
								airlineName.setName(builder.toString().trim());
							} else if ("AirlineName".equals(currentName)) {
								airlineNameMap.put(airlineName.getLanguageCode(), airlineName);
								airlineName = null;
							}
						}

						if ("TFCode".equals(currentName)) {
							airline.setTfCode(builder.toString().trim());
						} else if ("IataCode".equals(currentName)) {
							airline.setIataCode(builder.toString().trim());
						} else if ("Airline".equals(currentName)) {
							airline.setAirlineNameMap(airlineNameMap);
							airlineMap.put(airline.getTfCode(), airline);
							airline = null;
						}
					} else if ("AirlineList".equals(currentName)) {
						break done;
					}
					currentName = null;
					break;
			}
		}

		XGetAirlinesDataResponse response = new XGetAirlinesDataResponse();
		response.setAirlineMap(airlineMap);
		return response;
	}

}
